//
//  ZLStatusPictureView.swift
//  ZLWeibo
//
//  Created by zhanglongtao on 16/12/28.
//  Copyright © 2016年 hanju001. All rights reserved.
//

import UIKit

class ZLStatusPictureView: UIView {

    @IBOutlet weak var heightCons: NSLayoutConstraint!
    
    var Viewmodel: ZLStatusViewModel? {
        
        didSet{
            
            caculateSize()
            
            //设置urls
            urls = Viewmodel?.picUrls
            
        }
    }
    private func caculateSize(){
        
        if Viewmodel?.picUrls?.count == 1{
            
            let viewSize = Viewmodel?.pictureViewSize ?? CGSize()
            let v = subviews[0]
            v.frame = CGRect(x: 0, y: 0, width: viewSize.width, height: viewSize.height)
            
        }else{
            //恢复尺寸
            let v = subviews[0];
            v.frame = CGRect(x: 0, y: 0, width: ZLStatusPictureItemWidth, height: ZLStatusPictureItemWidth)
            
        }
        
        heightCons.constant = Viewmodel?.pictureViewSize.height ?? 0;
    }
    
    private var urls: [ZLStatusPicture]? {
        didSet{
            
            //隐藏所有的iamgeView
            for iv in subviews{
                
                iv.isHidden = true
                
            }
            var index = 0
            for url in urls ?? [] {
                
                //获得对应索引的imageView
                let iv = subviews[index] as! UIImageView
                if index == 1 && urls?.count == 4{
                    index += 1
                }
                iv.hm_setImage(urlString: url.thumbnail_pic, placeholder: nil)
                
                //判断是不是gif
                iv.subviews[0].isHidden = (((url.thumbnail_pic ?? "") as NSString).pathExtension.lowercased() != "gif")
                
                print("++++++++++\(url.thumbnail_pic)")
                iv.isHidden = false
                
                index += 1
                
            }
        }
        
    }
    override func awakeFromNib() {
        
        setupUI()
    }
    // MARK --- 监听方法
    @objc fileprivate func tapImageView(tap: UITapGestureRecognizer){
        
        guard let iv = tap.view as? UIImageView, let picUrls = Viewmodel?.picUrls else{
            
            return
        }
        var slectedindex = iv.tag
        //针对四张图处理
        if picUrls.count == 4 && slectedindex > 1{
            slectedindex -= 1
        }
        let urls = (picUrls as NSArray).value(forKey: "largePic") as! [String]
        //处理可见的图像视图
        var imageViewList = [UIImageView]()
        
        for iv in subviews as! [UIImageView]{
            if !iv.isHidden{
                
                imageViewList.append(iv)
            }
        }
        //发送通知
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: ZLStatusCellBrowserPhotoNotification), object: self, userInfo: [ZLSatatusCellBrowserPhotoSlectedIndexKey: slectedindex, ZLSatatusCellBrowserPhotoURLSKey: urls, ZLSatatusCellBrowserPhotoImageViewsKey: imageViewList])
        
    }
}

extension ZLStatusPictureView {
    
    //cell中的控件都是提亲创建准备好  设置的时候，根据数据决定显示
    //不要动态创建
    
    fileprivate func setupUI(){
        let count = 9
        
        //设置背景颜色
        backgroundColor = UIColor.clear
        
        //超出边界的内容不显示
        clipsToBounds = true
        let rect = CGRect(x: 0, y: 0, width: ZLStatusPictureItemWidth, height: ZLStatusPictureItemWidth)
        for i in 0..<count{
            
            let iv = UIImageView()
            iv.backgroundColor = super.backgroundColor
            
            //行
            let row = CGFloat(i / 3)
            //列
            let col = CGFloat(i % 3)
            let xoffset = col * (ZLStatusPictureItemWidth + ZLStatusPictureInnerMargin)
            let yoffset = row *  (ZLStatusPictureItemWidth + ZLStatusPictureInnerMargin)
            iv.frame = rect.offsetBy(dx: xoffset, dy: yoffset)
            
            addSubview(iv)
            
            //网imageView能够接受用户交互
            iv.isUserInteractionEnabled = true
            let tap = UITapGestureRecognizer(target: self, action: #selector(tapImageView))
            iv.addGestureRecognizer(tap)
            //设置IamgeView的tag
            iv.tag = i
            
            addGifView(iv: iv)
        }
        
    }
    
    private func addGifView(iv: UIImageView){
        
        let gifImageView = UIImageView(image: UIImage(named: "timeline_image_gif"))
        
        iv.addSubview(gifImageView)
        gifImageView.translatesAutoresizingMaskIntoConstraints = false
        iv.addConstraint(NSLayoutConstraint(item: gifImageView, attribute: .bottom, relatedBy: .equal, toItem: iv, attribute: .bottom, multiplier: 1.0, constant: 0))
        iv.addConstraint(NSLayoutConstraint(item: gifImageView, attribute: .right, relatedBy: .equal, toItem: iv, attribute: .right, multiplier: 1.0, constant: 0))
        
    }
    
}
